<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Report_group_model extends MY_Model {

	const TABLE = 'report_group';

	public function add($group_name,$report_id, $group_sort) {
		$data = array(
			'group_name'  => $group_name,
			'report_id'   => $report_id,
			'group_sort'  => $group_sort,
		);
		$this->db->insert(self::TABLE, $data);
		return $this->db->insert_id();
	}

	public function delete($group_id) {
		$this->db->where('group_id', $group_id);
		$this->db->delete(self::TABLE);
	}

	public function update($group_id, $group_name, $report_id, $group_sort) {
		$data = array(
			'group_name'  => $group_name,
			'report_id'   => $report_id,
			'group_sort'  => $group_sort,
		);
		$this->db->where('group_id', $group_id);
		$this->db->update(self::TABLE, $data); 
	}

	public function get_group_list() {
		$this->db->join('report', 'report.report_id = report_group.report_id');
		$this->db->order_by("report_name asc", "group_sort asc");
		$query = $this->db->get('report_group');
		return $query->result_array();
	}

	public function get_group_by_name($group_name) {
		$query = $this->db->get_where(self::TABLE, array('group_name' => $group_name));
		return $query->row_array();
	}

	public function get_group_by_id($group_id) {
		$query = $this->db->get_where(self::TABLE, array('group_id' => $group_id));
		return $query->row_array();
	}

	public function get_group_id($report_id) {
		$query = $this->db->get_where(self::TABLE, array('report_id' => $report_id));
		return $query->result_array();
	}

	public function get_first_row() { 
		$query = $this->db->query("select group_id from report_group order by group_id ASC limit 0,1");
		return $query->row_array();
	}
}